package com.course.business.controller.admin;

import com.course.server.dto.CategoryDto;
import com.course.server.dto.TeacherDto;
import com.course.server.dto.PageDto;
import com.course.server.dto.ResponseDto;
import com.course.server.service.TeacherService;
import com.course.server.utils.ValidatorUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 〈一句话功能简述>
 * 〈〉
 *
 * @Author metype
 * @Create 2021/5/25
 * @Since 1.0.0
 */
@RestController
@RequestMapping("/admin/teacher")
public class TeacherController {

    private static final Logger LOG = LoggerFactory.getLogger(TeacherController.class);
    public static final String BUSINESS_NAME = "讲师";

    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/list")
    public ResponseDto<PageDto> test(@RequestBody PageDto pageDto) {
        LOG.info("pageDto:{}", pageDto);
        teacherService.list(pageDto);
        return new ResponseDto <>(pageDto);
    }

    @RequestMapping("/save")
    public ResponseDto<TeacherDto> save(@RequestBody TeacherDto teacherDto) {
        LOG.info("teacherDto:{}", teacherDto);
        // 保存校验
        ValidatorUtil.require(teacherDto.getName(), "名称");
        ValidatorUtil.length(teacherDto.getName(), "名称", 1 , 50);
        ValidatorUtil.length(teacherDto.getNickname(), "昵称", 1 , 50);
        ValidatorUtil.length(teacherDto.getImage(), "头像", 1 , 100);
        ValidatorUtil.length(teacherDto.getPosition(), "职位", 1 , 50);
        ValidatorUtil.length(teacherDto.getMotto(), "座右铭", 1 , 50);
        ValidatorUtil.length(teacherDto.getIntro(), "简介", 1 , 500);

        teacherService.save(teacherDto);
        return new ResponseDto <>(teacherDto);
    }

    @GetMapping("/del/{id}")
    public ResponseDto del(@PathVariable String id) {
        LOG.info("id:{}", id);
        teacherService.del(id);
        return new ResponseDto<>("200","删除成功！");
    }

    @GetMapping("/all")
    @ApiOperation("获取讲师信息")
    public ResponseDto <List <TeacherDto>> all() {
        ResponseDto <List <TeacherDto>> responseDto = new ResponseDto <>();
        List <TeacherDto> teacherList = teacherService.all();
        responseDto.setContent(teacherList);
        return responseDto;
    }
}